#ifndef __RN8306_H
#define __RN8306_H

#include "hc32f460.h"
#include <rtthread.h>
#include <rtdevice.h>
#include "board_config.h"
#include <string.h>
#include "hc32_ll_pwc.h"

#define ENABLE_FCG0_REG_WRITE()             (M4_MSTP->FCG0PC = 0xa5a50001u)
#define DISABLE_FCG0_REG_WRITE()            (M4_MSTP->FCG0PC = 0xa5a50000u)

/****************************************/
#define RN8306_SPI_BUS_NAME         "spi2"
#define RN8306_SPI_DEVICE_NAME      "RN8306Data"

#define RN8306_SPI2_CS_PORT                (GPIO_PORT_A)
#define RN8306_SPI2_CS_PIN                 (GPIO_PIN_08)
                      
#define RN8306_SPI2_SCK_PORT               (GPIO_PORT_A)
#define RN8306_SPI2_SCK_PIN                (GPIO_PIN_09)
#define RN8306_SPI2_SCK_FUNC               (Func_Spi2_Sck)
                      
#define RN8306_SPI2_MOSI_PORT              (GPIO_PORT_C)
#define RN8306_SPI2_MOSI_PIN               (GPIO_PIN_09)
#define RN8306_SPI2_MOSI_FUNC              (Func_Spi2_Mosi)
                      
#define RN8306_SPI2_MISO_PORT              (GPIO_PORT_A)
#define RN8306_SPI2_MISO_PIN               (GPIO_PIN_10)
#define RN8306_SPI2_MISO_FUNC              (Func_Spi2_Miso)

#define SPI2_CS_PORT_PIN        RN8306_SPI2_CS_PORT , RN8306_SPI2_CS_PIN   //  GET_PIN(A, 8)
#define SPI2_CS_PPIN            GET_PIN(A, 8)
#define SPI2_SCK_PIN            RN8306_SPI2_SCK_PORT , RN8306_SPI2_SCK_PIN   //GET_PIN(A, 9)
#define SPI2_MOSI_PIN           RN8306_SPI2_MOSI_PORT , RN8306_SPI2_MOSI_PIN  //GET_PIN(C, 9)
#define SPI2_MISO_PIN           RN8306_SPI2_MISO_PORT , RN8306_SPI2_MISO_PIN  //GET_PIN(A, 10)


/*SPI3 DMA*/
#define RN8306_DMA_SPI_BUS_NAME         "spi3"
#define RN8306_DMA_SPI_DEVICE_NAME      "RN8306DMA"

#define RN8306_SPI3_CS_PORT                (GPIO_PORT_C)
#define RN8306_SPI3_CS_PIN                 (GPIO_PIN_06)
                      
#define RN8306_SPI3_SCK_PORT               (GPIO_PORT_C)
#define RN8306_SPI3_SCK_PIN                (GPIO_PIN_08)
#define RN8306_SPI3_SCK_FUNC               (Func_Spi3_Sck)
                      
#define RN8306_SPI3_MOSI_PORT              (GPIO_PORT_C)
#define RN8306_SPI3_MOSI_PIN               (GPIO_PIN_07)
#define RN8306_SPI3_MOSI_FUNC              (Func_Spi2_Mosi)

#define SPI3_CS_PORT_PIN        RN8306_SPI3_CS_PORT , RN8306_SPI3_CS_PIN   //  GET_PIN(A, 8)
#define SPI3_CS_PPIN            GET_PIN(C, 6)
#define SPI3_SCK_PIN            RN8306_SPI3_SCK_PORT  , RN8306_SPI3_SCK_PIN   //GET_PIN(A, 9)
#define SPI3_MOSI_PIN           RN8306_SPI3_MOSI_PORT , RN8306_SPI3_MOSI_PIN  //GET_PIN(C, 9)
#define SPI3_MISO_PIN           RN8306_SPI3_MISO_PORT , RN8306_SPI3_MISO_PIN  //GET_PIN(A, 10)


/************************************************************************/
typedef struct
{
	uint16_t addr;
	char   *name;
	uint32_t rst;
	uint32_t set;    
	uint16_t ee_addr;	//校表寄存器存在EE中（注意仅限校表参数）
	uint8_t  len;		//有效长度
	uint8_t  mod;		//参数类型（0=取复位值,1=取EE值,2=取固定值,3=不操作,4=特殊处理）
}ST_REG_PARA;


extern const ST_REG_PARA EC_EMU_RegData[];
extern char u8RxBuffer[1352];
extern char u8TxBuffer[128];

void RN8306_Wave_Init();
void RN8306_DMA_thread_entry(void *parameter);
void RN8306_thread_entry(void *parameter);
/*********************************************/
#endif